{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit AudioDescriptor;

interface

uses Classes, Sounds, Descriptor;

const
  FILE_AUDIO_DESCRIPTOR = 'AudioDescriptor.xml';

type
  TAHMAudioDescriptor = class(TAHMDescriptor)
  private
    FSoundList: TAHMSoundList;
  protected
    procedure ParseXML; override;
    procedure SaveAsXML(Lines: TStrings); override;
    procedure SetBaseDescriptor(Base: TAHMDescriptor); override;
    function GetSaveRequired: Boolean; override;
  public
    constructor Create; override;
    destructor Destroy; override;
    property SoundList: TAHMSoundList read FSoundList;
    procedure Initialise; override;
    procedure Assign(Source: TAHMDescriptor); override;
    procedure Sort; override;
  end;


implementation

uses TagNames, XMLTags, ErrorCodes, SysUtils, XMLHelper;

constructor TAHMAudioDescriptor.Create;
begin
  inherited Create;

  FFileName := FILE_AUDIO_DESCRIPTOR;
  FRootTagName := TAG_AUDIO_DESCRIPTOR;

  FSoundList := TAHMSoundList.Create;

  // Audio descriptors are not mandatory parts of the theme
  FMandatory := False;
end;

destructor TAHMAudioDescriptor.Destroy;
begin
  FSoundList.FreeWithContents;

  inherited Destroy;
end;

procedure TAHMAudioDescriptor.SetBaseDescriptor(Base: TAHMDescriptor);
begin
  // Set base list property for each of our list objects
  with Base as TAHMAudioDescriptor do
    Self.FSoundList.BaseList := SoundList;
end;

function TAHMAudioDescriptor.GetSaveRequired: Boolean;
begin
  // Save required if any of the lists are populated
  Result := FSoundList.RequiresSaving;
end;

procedure TAHMAudioDescriptor.ParseXML;
var
  RootNode: TAHMXMLNode;
begin
  // What nodes do we have here?
  RootNode := FHelper.CurrentNode;
  FParseNode := nil;

  while FHelper.NextElement(RootNode, FParseNode) do
  begin
    if FHelper.CurrentElement = TAG_SOUNDS then
      FSoundList.ParseFromXML(FHelper)
    else
      LogParsingError(THM_PRS_AUD_FOUND_UNKNOWN_CONTENT_TAG);
  end;
end;

procedure TAHMAudioDescriptor.SaveAsXML(Lines: TStrings);
begin
  // Write all audio descriptor lists to XML file
  FSoundList.SaveAsXML(Lines);
end;

procedure TAHMAudioDescriptor.Initialise;
begin
  inherited;

  // Load required sounds for descriptor
  FSoundList.LoadSounds;
end;

procedure TAHMAudioDescriptor.Assign(Source: TAHMDescriptor);
begin
  inherited;

  // Copy properties from audio descriptor
  with Source as TAHMAudioDescriptor do
  begin
    Self.SoundList.AssignContents(SoundList);
  end;
end;

procedure TAHMAudioDescriptor.Sort;
begin
  inherited;

{$IFDEF DESIGNER}
  FSoundList.SortObjects;
{$ENDIF}
end;

end.


